package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import jdk.jpackage.internal.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;



    /**
     * 登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 插入新员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO){

        System.out.println("当前线程的id:"+Thread.currentThread().getId());

        log.info("新增员工：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }


    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    //  /** enter 产生函数注释
    @GetMapping("/page")//接口地址,区分 getmapping 和 postmapping
    @ApiOperation("员工分页查询")//功能名称
    //分页查询前端数据格式来自多个控件，不是json，不加@RequestBody注解
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        //DTO表示来自前端的数据
        log.info("员工分页查询参数:{}",employeePageQueryDTO);
        //员工分页查询参数:EmployeePageQueryDTO(name=null, page=2, pageSize=10)查询从第2页开始的10条与name相关的记录
        //输出前端参数
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        //对于employee表的建立新的功能函数
        //使用alt enter 建立一个新的方法函数
        return Result.success(pageResult);
        //Result定义时使用了通用的数据类型
    }


    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")//修改类请求
    @ApiOperation("启用禁用员工账号")
    public Result StartOrStop(@PathVariable("status") Integer status,Long id){
        //status参数是/status/{status}从地址中提取的地址参数，要加地址参数注释@PathVariable
        //id 是地址栏query传递的参数,不需要注释
        log.info("启用禁用员工账号参数:{},{}",status,id);//调试打印
        employeeService.StartOrStop(status,id);
        return Result.success();//返回成功信息
    }


    /**
     * 查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询员工信息")
    public Result<Employee> GetById(@PathVariable("id") Long id){
        log.info("启用禁用员工账号参数:{}",id);//调试打印
        Employee employee = employeeService.GetById(id);
        return Result.success(employee);//返回成功信息
    }


    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping//请求类型不要写错
    @ApiOperation("编辑员工信息")
    public Result Update(@RequestBody EmployeeDTO employeeDTO){
        //employeeDTO是json类型的参数,需要RequestBody注释
        log.info("编辑员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();//返回成功信息
    }


    //新功能的起点
    //后端要返回给前端的数据类型+功能函数名+前端传给后端的数据类型
    //前端数据的封装由SpringMVC框架完成

}
